package com.cg.javafxdemospring.dao;

import com.cg.javafxdemospring.util.DateUtil;
import com.cg.javafxdemospring.entity.TzfcPersonInfo;
import javafx.scene.control.TextArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 陈刚
 * @ClassName ImportDao
 * @Description //TODO
 * @create 2020-08-06 17:08
 */
@Repository
public class ImportDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    String sqlTemp = "INSERT INTO [dbo].[sampleresult]([barcode], [hosbarcode], [pname], [psexname], [Psextype]," +
            " [page], [pagetype], [sampletime], [hosapplycode], [hossubitemcode], [hosapplyitemname], [hositemname]," +
            " [applycode], [applyitemName], [subitemcode], [Itemname], [Testresult], [Unit], [HLflag]," +
            " [Referencedown], [Referenceup], [Referencevalue], [Hosremark], [Sampletypename], [Sendtesttime]," +
            " [reAuthorizortime], [ReAuthorizeoperatename], [Checktime], [Authorizeoperatename], [ReceiveTime], [Remarks], [status])" +
            " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";


    public List<TzfcPersonInfo> listTzfcPersonInfo(List<String> barcodes) {
        Map<String, Object> paramMap = new HashMap<String, Object>();

        String getTzfcSql = "SELECT * FROM tzfc_Person_info where barcode in(:barcodes)";
        paramMap.put("barcodes", barcodes);
        List<TzfcPersonInfo> tzfcPersonInfos = namedParameterJdbcTemplate.queryForList(getTzfcSql, paramMap, TzfcPersonInfo.class);
        return tzfcPersonInfos;
    }

    public void syncData(List<List<String>> lists, TextArea textArea) {
        List<String> barcodes = lists.stream().map(list -> list.get(11)).collect(Collectors.toList());
        List<TzfcPersonInfo> tzfcPersonInfos = listTzfcPersonInfo(barcodes);
        Map<String, TzfcPersonInfo> tzfcPersonInfoMap = tzfcPersonInfos.stream().collect(Collectors.toMap(TzfcPersonInfo::getBarcode, Function.identity()));
        List<String> existBarcodes = jdbcTemplate.queryForList("select distinct barcode from sampleresult",String.class);
        for (List<String> list : lists) {
            String barcode = list.get(11);
            //跳过已经导入过的数据
            if (existBarcodes.contains(barcode)) {
                textArea.appendText(barcode+"记录已存在，跳过导入\n");
                continue;
            }
            String s0 = list.get(0);
            String idcard = list.get(1);
            String pname = list.get(2);
            String psexname = list.get(3);
            String page = list.get(4);
            String s5 = list.get(5);
            String sendtesttime = list.get(6);
            String s7 = list.get(7);
            String hosbarcode = list.get(8);
            String s9 = list.get(9);
            String s10 = list.get(10);
            TzfcPersonInfo tzfcPersonInfo = tzfcPersonInfoMap.get(barcode);
//            if (true) {
//                continue;
//            }
            jdbcTemplate.update(sqlTemp,new PreparedStatementSetter(){

                private void setObjects(PreparedStatement preparedStatement, Object...objects) throws SQLException {
                    for (int i = 0; i < objects.length; i++) {
                        preparedStatement.setObject(i+1,objects[i]);
                    }
                }

                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    String pageType = "1";
                    Date sampletime = null;
                    String hosapplycode ="2942";
                    String hossubitemcode = "2942";
                    String hosapplyitemname = "新型冠状病毒核酸检测";
                    String hositemname = "新型冠状病毒核酸检测";

                    String applycode = "PCRMD-2019-nCoV";
                    String applyitemName = "新型冠状病毒核酸检测";
                    String subitemcode = "PCR-2019-nCoV";
                    String Itemname = "新型冠状病毒核酸检测";
                    String Testresult = "未检出";
                    String Unit = "";
                    String HLflag = "";
                    String Referencedown = "未检出";
                    String Referenceup = "未检出";
                    String Referencevalue = "";//""3.5E+02Copies/mL";
                    String Hosremark = null;
                    String Sampletypename = "咽拭子";
                    Date Sendtesttime = DateUtil.stringToDate(sendtesttime,"yyyy.MM.dd");///
                    Date reAuthorizortime = new Date();///
                    String ReAuthorizeoperatename = "付玉";
                    Date Checktime = new Date();///
                    String Authorizeoperatename = "曹丽丽";
                    Date ReceiveTime = new Date();
                    String Remarks = idcard;
                    String status = "";
                    if (tzfcPersonInfo != null) {
                        pageType = tzfcPersonInfo.getPagetype();
                        sampletime = tzfcPersonInfo.getSampletime();
//                        hosapplycode = tzfcPersonInfo.co
//                        hossubitemcode = tzfcPersonInfo.ge;
//                        hosapplyitemname = tzfcPersonInfo.get;
//                        hositemname = tzfcPersonInfo.getnam;

//                        applycode = tzfcPersonInfo.getco;
//                        applyitemName = tzfcPersonInfo.geta;
//                        subitemcode = tzfcPersonInfo.gets;
//                        Itemname = tzfcPersonInfo.geti;
//                        Testresult = tzfcPersonInfo.getre;
//                        Unit = tzfcPersonInfo.getu;
//                        HLflag = tzfcPersonInfo.geth;
//                        Referencedown = tzfcPersonInfo.getre;
//                        Referenceup = tzfcPersonInfo.getre;
//                        Referencevalue = tzfcPersonInfo.getre;
//                        Hosremark = tzfcPersonInfo.getRemark();
//                        Sampletypename = tzfcPersonInfo.getsa;
//                        Sendtesttime = tzfcPersonInfo.sen;
//                        reAuthorizortime = tzfcPersonInfo.re;
//                        ReAuthorizeoperatename = tzfcPersonInfo.re;
//                        Checktime = tzfcPersonInfo.ch;
//                        Authorizeoperatename = tzfcPersonInfo.au;
//                        ReceiveTime = tzfcPersonInfo.getre;
                        Remarks = tzfcPersonInfo.getRemark();
//                        status = tzfcPersonInfo.gets;
                    }
                    setObjects(preparedStatement, barcode, hosbarcode, pname,psexname,null,page,
                            pageType, sampletime, hosapplycode, hossubitemcode, hosapplyitemname, hositemname, applycode, applyitemName,
                            subitemcode, Itemname, Testresult, Unit, HLflag, Referencedown, Referenceup, Referencevalue,Hosremark,
                            Sampletypename, Sendtesttime,reAuthorizortime,ReAuthorizeoperatename,Checktime,Authorizeoperatename,
                            ReceiveTime,Remarks,status
                    );
//                    preparedStatement.setObject(1,s11);
//                    preparedStatement.setObject(2,s8);
//                    preparedStatement.setObject(3,s8);
                }
            });
//            Platform.runLater(()->textArea.appendText(barcode+"记录同步完成\n"));
//            new Thread(()->textArea.appendText(barcode+"记录同步完成\n")).start();
            textArea.appendText(barcode+"记录同步完成\n");

        }
        textArea.appendText("记录全部同步完成\n");
    }
}
